package com.task.core.service.impl;

import java.util.List;
import javax.annotation.Resource;

import com.task.common.enums.QuestionOperationTypeEnum;
import com.task.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.task.common.annotation.DataScope;
import com.task.common.domain.info.KeyAndValue;
import com.task.common.enums.QuestionInfoTypeEnum;
import com.task.common.exception.ServiceException;
import com.task.common.utils.SecurityUtils;
import com.task.common.utils.uuid.UUID;
import com.task.core.domain.EngineInfo;
import com.task.core.dto.request.EngineRequestDTO;
import com.task.core.dto.request.QuestionInfoRequestDTO;
import com.task.core.mapper.EngineMapper;
import com.task.core.service.EngineService;
import com.task.core.service.QuestionInfoService;
import org.springframework.util.ObjectUtils;

/**
 * @author 迪迦.
 * @date 2024/11/4 17:26
 */
@Service("EngineService")
public class EngineServiceImpl implements EngineService {

  @Resource
  private EngineMapper engineMapper;

  @Resource
  private QuestionInfoService questionInfoService;

  @Override
  //@DataScope(deptAlias = "dept", userAlias = "su")
  public List<EngineInfo> getEngineByPage(EngineRequestDTO request) {
    //request.setOperationBy(SecurityUtils.checkAdmin());
    return engineMapper.getEngineByPage(request);
  }

  @Override
  //@DataScope(deptAlias = "dept", userAlias = "su")
  public List<KeyAndValue> getEngineForOption(EngineRequestDTO request) {
    //request.setOperationBy(SecurityUtils.checkAdmin());
    return engineMapper.getEngineForOption(request);
  }

  @Transactional
  @Override
  public void addEngine(EngineRequestDTO request) {
    request.setOperationBy(SecurityUtils.getLoginUser().getUserId());
    request.setCode(UUID.randomUUID().toString().replace("-", ""));
    request.setVersionCode(UUID.randomUUID().toString().replace("-", ""));
    engineMapper.addEngine(request);
//    questionInfoService.insertQuestion(new QuestionInfoRequestDTO(QuestionInfoTypeEnum.ENGINE, request.getCode(), ""));
  }

  @Transactional
  @Override
  public void modifyEngine(EngineRequestDTO request) {
    request.setOperationBy(SecurityUtils.getLoginUser().getUserId());
    request.setVersionCode(UUID.randomUUID().toString().replace("-", ""));
    Integer integer = engineMapper.modifyEngine(request);
    if(integer == 0) {
      throw new ServiceException("code不存在");
    }
    Boolean isForcePush = request.getIsForcePush();
    if(!ObjectUtils.isEmpty(isForcePush) && isForcePush){
      questionInfoService.insertQuestion(new QuestionInfoRequestDTO(QuestionInfoTypeEnum.ENGINE, request.getCode(),"",request.getPromptCode(), QuestionOperationTypeEnum.UPDATA.getCode()));
    }
  }

  @Override
  public void deleteEngine(String engineCode, Long userId) {
    Integer integer = engineMapper.deleteEngine(engineCode, userId);
    if(integer == 0) {
      throw new ServiceException("code不存在");
    }
    //删除对应的关系表
    //删除设备和引擎的关联关系
    engineMapper.deleteDeviceRelation(engineCode);
    //删除引擎和引擎方案的关联关系
//    engineMapper.deleteEngineSchemeRelation(engineCode);
    questionInfoService.insertQuestion(new QuestionInfoRequestDTO(QuestionInfoTypeEnum.ENGINE, engineCode, "",QuestionOperationTypeEnum.DELETE.getCode()));
  }

  @Override
  public EngineInfo getEngineByCode(String code) {
    return engineMapper.getEngineByCode(code);
  }

  @Override
  public List<EngineInfo> getEngineByCodes(List<String> codes) {
    return engineMapper.getEngineInfoByCodes(codes);
  }

  @Override
  public List<String> getAllCodes() {
    return engineMapper.getAllCodes();
  }

  @Override
  public Integer getCountByVersionCodes(List<String> versionCodes) {
    return engineMapper.getCountByVersionCodes(versionCodes);
  }
}
